package com.oauth.auth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.stream.Collectors;

@SpringBootApplication
public class AuthApplication {

    public static void main(String[] args) {
        SpringApplication.run(AuthApplication.class, args);
    }

    @Autowired
    DataSource dataSource;

    @PostConstruct
    public void init() {
        JdbcClientDetailsService jdbcClientDetailsService = new JdbcClientDetailsService(dataSource);
        jdbcClientDetailsService.setPasswordEncoder(new BCryptPasswordEncoder());
        BaseClientDetails clientDetails = new BaseClientDetails();
        clientDetails.setClientId("shnice");
        clientDetails.setClientSecret("qaz123");
        clientDetails.setResourceIds(Arrays.asList("resource"));
        clientDetails.setAuthorizedGrantTypes(Arrays.asList("authorization_code", "refresh_token"));
        clientDetails.setScope(Arrays.asList("all"));
        clientDetails.setRegisteredRedirectUri(Arrays.asList("https://www.shnice.club").stream().collect(Collectors.toSet()));
        jdbcClientDetailsService.addClientDetails(clientDetails);
    }

}
